package team.product.wapper;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.ResourceBundle;
import java.util.logging.Logger;


/**
 * Created with IntelliJ IDEA.
 * User: Lương Xuân Huy
 * Date: 9/4/14
 * Time: 4:07 PM
 * Long Van Soft Solution Sjc.
 * To change this template use File | Settings | File and Code Templates
 */
public class ForumRequestWrapper extends HttpServletRequestWrapper {
    protected HttpServletRequest servletRequest;
    protected ResourceBundle resourceBundle;
    private boolean socialLogin = false;
    private Logger logger = Logger.getLogger(getClass().getName());



    public ForumRequestWrapper(HttpServletRequest request) {
        super(request);
        this.servletRequest = request;
    }

    public void setSocialLogin(boolean socialLogin) {
        this.socialLogin = socialLogin;
    }

    public void addValidateError(String componentId, String messageId, String errorMessage) {
        FacesContext.getCurrentInstance().addMessage(componentId, new FacesMessage(FacesMessage.SEVERITY_ERROR, getMessageFromBundle(messageId, errorMessage), null));
    }

    public String getMessageFromBundle(String messageId, String messageDetail) {
        String message = null;
        if (null != messageId && null != getBundle()) message = getBundle().getString(messageId);
        if (null == message) message = messageDetail;
        logger.warning("Unable found message bundle,bundleName=" + getBundleName() + ",messageId" + messageId);
        return message;
    }

    public ResourceBundle getBundle() {
        {
            if (resourceBundle == null) {
                FacesContext context = FacesContext.getCurrentInstance();
                resourceBundle = context.getApplication().getResourceBundle(context, getBundleName());
            }
            return resourceBundle;
        }
    }

    public String getBundleName() {
        return "pt";
    }
}
